<template>
	<view class="contont">
		<nav-bar :title="title" :iconLeft="true" />
		<view class="content-bgc">
			<custom-list-de :record="detail_record" :columns="columns">
				<template #picture="{ record }">
					<image v-if="record.picture" :src="record.picture" class="preview-img"
						@click="previewImage(record.picture)" />
				</template>
				<template #marriage="{ record }">
					{{ record.marriage == 1 ? '未婚' : (record.marriage == 2 ? '已婚' : '离异') }}
				</template>
			</custom-list-de>
			<view class="btn-box" v-show="applyDetail == 'apply'">
				<up-button icon="close" type="error" text="拒 绝" @click="applyBtn(3)" />
				<up-button icon="checkmark" type="primary" text="通 过" @click="applyBtn(2)" />
			</view>
			<custom-popup :popupShow="popupShow" mode="center" :title="'审批：' + titlePop" width="96%" :closeable="false"
				:closeOnClickOverlay="false">
				<view style="background-color: #fff; padding: 20rpx 30rpx;">
					<uni-forms labelPosition="left" :model="form" ref="refForm" labelWidth="110px">
						<uni-forms-item label="审批意见" name="apply_content">
							<uni-easyinput type="textarea" v-model="form.apply_content" placeholder="请输入审批意见" />
						</uni-forms-item>
					</uni-forms>
					<view class="btn-box" style="justify-content: end;">
						<view>
							<up-button size="small" icon="close-circle" text="取 消" @click="cancal" />
						</view>
						<view v-show="form.status == 2">
							<up-button size="small" icon="checkmark" type="primary" text="通 过" @click="save" />
						</view>
						<view v-show="form.status == 3">
							<up-button size="small" icon="close" type="error" text="拒 绝" @click="save" />
						</view>
					</view>
				</view>
			</custom-popup>
		</view>
	</view>
</template>
<script setup>
import { ref, reactive } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import conContractApi from '../../../api/contract/con_contract'

const detail_record = ref({})
const title = '合同审核详情';
const applyDetail = ref('')
onLoad(option => {
	get_detail(option.id);
	applyDetail.value = option.type
	form.id = option.id
})
const columns = [
	// { title: "照片", dataIndex: 'picture', scopedSlots: 'picture' },
	{ title: "编号", dataIndex: 'code' },
	{ title: "合同名称", dataIndex: 'name' },
	{ title: "合同类型", dataIndex: 'classify_name' },
	{ title: "甲方", dataIndex: 'party_a' },
	{ title: "签署日期", dataIndex: 'sign_date' },
	{ title: "生效日期", dataIndex: 'start_date' },
	{ title: "到期日期", dataIndex: 'end_date' },
	{ title: "备注", dataIndex: 'desc', note: true, },
]
function get_detail(id) {
	conContractApi.contract_detail(id).then(data => {
		detail_record.value = data
		titlePop.value = data.name
	})
}
const previewImage = (img) => {
	// uniapp 原生预览图片方法
	uni.previewImage({
		current: img.url || img,  // 当前显示图片
		urls: [img.url || img]    // 图片列表
	})
}

function applyBtn(status) {
	form.status = status
	popupShow.value = true
}

const refForm = ref(null);
const form = reactive({
	id: null,
	apply_content: '',
	status: ''
});
const titlePop = ref('弹窗')
const popupShow = ref(false)
function save() {
	refForm.value.validate().then(() => {
		const params = uni.$lodash.cloneDeep(form) // 深拷贝一份form表单
		conContractApi.patch_contract(params, params.id).then((res) => {
			uni.$u.toast('修改成功')
			setTimeout(() => {
				backRefsh();
			}, 1000)
		})

	}).catch((error) => {
		uni.$u.toast(error[0].errorMessage);
	})
};

function cancal() {
	popupShow.value = false
	Object.assign(form, {
		id: null,
		user: '',
		start_time: '',
		end_time: '',
		content: '',
	})
}
//返回上一级并刷新上一级页面
function backRefsh() {
	uni.navigateBack({
		delta: 1, // 返回的页面层数，1 表示返回上一级
		success: () => {
			uni.$bus.useBusEmit('con_apply');
		}
	})
};
</script>

<style lang="scss"></style>